import { FileUtils, TFileUtils } from "./fileutils";
import { BuildPlatform } from "./CDNBuildOptions";
import { CDNClientConfigs } from "./CDNConfigs";
import { UploadUtils } from "./TCDNUploadFlowPorts";


class Upload {

	utils: any;


	configs: CDNClientConfigs = {
		test: {
			accessKeyId: "",
			secretAccessKey: '',
			region: "",
			bucket: "",
			downloadUrl: "",
			downloadPath: "",
			allowOverwirte: true,
		},
		pretest: {
			accessKeyId: "",
			secretAccessKey: '',
			region: "",
			bucket: "",
			downloadUrl: "",
			downloadPath: "",
			allowOverwirte: false,
		},
		release: {
			accessKeyId: "",
			secretAccessKey: '',
			region: "",
			bucket: "",
			downloadUrl: "",
			downloadPath: "",
			allowOverwirte: false,
		},
	}


	uploadWebMobile = async () => {
		FileUtils.modifyFile({
			path: "index.html",
			expr: "<body>",
			replace: '<body style="margin: 0%;">',
		})
		await FileUtils.pngQuant({
			fileGlob: "assets/**/*.png",
		})
		await UploadUtils.uploadPlatformFolder()
		await UploadUtils.displayWebUrl()
		await UploadUtils.displayWebUrlQRCode()
	}

	uploadWechatGame = async () => {
		let downloadUrl = await UploadUtils.fetchDownloadUrl()

		FileUtils.modifyFile({
			path: "application*.js",
			expr: /var server = \'.*\'/mg,
			replace: `var server = '${downloadUrl}'`,
		})
		await FileUtils.replaceFile({
			fileGlob: "src/assets/script/deps/gdk*.js",
			replace: "../../../configs/MiniGameLibs/wechat.mini.js",
		})
		await FileUtils.wrapFileContent({
			fileGlob: "src/assets/script/deps/gdk*.js",
			wrapContent: `(function (root) {
	  var exports = undefined,
	      module = undefined,
	      require = undefined;
	  var define = undefined;
	  var gdk = root;
	  (function () {
		  `+ "${rawcontent}" + `
	  }).call(root);
	})( // The environment-specific global.
	function () {
	  if (typeof globalThis !== 'undefined') return globalThis;
	  if (typeof self !== 'undefined') return self;
	  if (typeof window !== 'undefined') return window;
	  if (typeof global !== 'undefined') return global;
	  if (typeof this !== 'undefined') return this;
	  return {};
	}.call(this));`
		})

		await FileUtils.wrapFileContent({
			fileGlob: "src/assets/script/deps/version*.js",
			wrapContent: "${rawcontent}" +
				`(function () {
			var Version = window["Version"];
			Version.setVersionCode(${UploadUtils.versionCode})
		})();`
		})


		await FileUtils.uglifyJS({
			fileGlob: "src/assets/script/deps/*.js",
		})
		await FileUtils.pngQuant({
			fileGlob: "assets/**/*.png",
		})
		await FileUtils.pngQuant({
			fileGlob: "remote/**/*.png",
		})
		await UploadUtils.uploadPlatformFolder()
		await UploadUtils.displayUrl()
		await UploadUtils.removeRemoteFolders()
	}

	uploadAndroid = async () => {
		let downloadUrl = await UploadUtils.fetchDownloadUrl()

		FileUtils.modifyFile({
			path: "assets/src/application*.js",
			expr: /let server = \'.*\'/mg,
			replace: `let server = '${downloadUrl}'`,
		})
		let buildOptions = UploadUtils.buildOptions
		await UploadUtils.uploadPlatformRes({
			outputName: `${buildOptions.outputName}/assets`,
			platform: buildOptions.platform,
			releasePlatform: buildOptions.releasePlatform,
		})
		await UploadUtils.displayUrl()
		await UploadUtils.removeRemoteFolders()
	}

	_upload = async () => {
		await FileUtils.execAsyncOn(UploadUtils.targetDir, async () => {
			UploadUtils.localUploadDir = ".."
			switch (UploadUtils.platform) {
				case BuildPlatform.WebMobile: {
					await this.uploadWebMobile()
					break
				}
				case BuildPlatform.WechatGame: {
					await this.uploadWechatGame()
					break
				}
				case BuildPlatform.Android: {
					await this.uploadAndroid()
					break
				}
				default: {
					throw new Error("invalid build platform:" + UploadUtils.platform)
				}
			}
		})
	}


	upload = async () => {
		try {
			console.log(UploadUtils);

			UploadUtils.setWithConfigs(this.configs)
			this._upload()
		} catch (e) {
			console.error(e)
		}
	}

	setUploadDefaultConfig() {
		UploadUtils.setWithConfigs(this.configs)
	}


}

export const UploadAbility = new Upload();